package beyjava.model;


import java.util.Vector;
import beyjava.dao.ActivityDAO;
import beyjava.dao.PersonDAO;
import beyjava.dao.ProjectDAO;
import beyjava.dao.TaskDAO;
import beyjava.daoImp.ActivityDAOImp;
import beyjava.daoImp.PersonDAOImp;
import beyjava.daoImp.ProjectDAOImp;
import beyjava.daoImp.TaskDAOImp;
import beyjava.view.ReportMenu;


/**
 * @author BEYJAVA data, which are came from controller class, are used at
 *         needed model layer methods.
 */
public class SystemQA {

	/**
	 * constructor of systemqa
	 */
	public SystemQA() {

	}

	/**
	 * @param prj
	 *            return true if project is added successfully
	 */
	public boolean addProject(Project prj) {
		boolean success = false;
		if (prj != null) {
			ProjectDAO projectDAO = new ProjectDAOImp();
			success = projectDAO.insertProject(prj);
		}
		return success;
	}

	/**
	 * @param currentProject
	 * @param newProject
	 * @return true if project is updated correctly
	 */
	public boolean updateProject(Project currentProject, Project newProject) {
		currentProject.updateProject(newProject);
		ProjectDAO projectDAO = new ProjectDAOImp();
		return projectDAO.updateProject(currentProject);
	}

	/**
	 * @param removedProject
	 * @return true if project is removed correctly
	 */
	public boolean removeProject(Project removedProject) {
		boolean success = false;
		if (removedProject != null) {
			ProjectDAO projectDAO = new ProjectDAOImp();
			success = projectDAO.removeProject(removedProject.getProjectID());
		}
		return success;
	}

	/**
	 * @return Vector<Project> project informations
	 */
	public Vector<Project> getProjects() {
		ProjectDAO projectDAO = new ProjectDAOImp();
		return projectDAO.getProjects();
	}

	/**
	 * @param projectID
	 * @return Project chosen project informations
	 */
	public Project getSelectedProject(int projectID) {
		ProjectDAO projectDAO = new ProjectDAOImp();
		return projectDAO.selectProjectByID(projectID);
	}

	/**
	 * @param newActivitity
	 * @return Activity the activity is null or not to be added.
	 */
	public Activity addActivity(Activity newActivitity) {
		Activity act = Activity.createActivity(newActivitity.getActNumber(),
				newActivitity.getActDesc(), newActivitity.getActStartDate(),
				newActivitity.getActDeliver(), newActivitity.getHours(),
				newActivitity.getActivityProject());
		if (act != null) {
			ActivityDAO activityDAO = new ActivityDAOImp();
			activityDAO.insertActivity(act);
			return act;
		}
		return act;
	}

	/**
	 * @param removedActitvity
	 * @return true if activity is removed correctly
	 */
	public boolean removeActivity(Activity removedActitvity) {
		ActivityDAO activityDAO = new ActivityDAOImp();
		return activityDAO.removeActivity(removedActitvity.getActNumber());
	}

	/**
	 * @param updatedActivity
	 * @param newActivity
	 * @return true if activity is updated successfully
	 */
	public boolean updateActivity(Activity updatedActivity, Activity newActivity) {
		updatedActivity.updadateActiviyOfProject(newActivity);
		ActivityDAO activityDAO = new ActivityDAOImp();
		return activityDAO.updateActivity(updatedActivity);
	}

	/**
	 * @param project
	 * @return Vector<Activity> activity informations of chosen project
	 */
	public Vector<Activity> getActivityOfProject(Project project) {
		if (project != null) {
			ActivityDAO activityDAO = new ActivityDAOImp();
			return activityDAO.getActivitiesOfProject(project.getProjectID());
		}
		return null;
	}

	/**
	 * @param activityID
	 * @return Activity informations of chosen activity
	 */
	public Activity getSelectedActivity(int activityID) {
		ActivityDAO activityDAO = new ActivityDAOImp();
		return activityDAO.getActivity(activityID);
	}

	/**
	 * @param newTask
	 * @return Task if it is not null
	 */
	public Task addTask(Task newTask) {
		Task task = Task.createTask(newTask.getTaskNumber(),
				newTask.getTaskDesc(), newTask.getTaskStartDate(),
				newTask.getTaskHours(), newTask.getActOfTask());
		if (task != null) {
			TaskDAO taskDOA = new TaskDAOImp();
			taskDOA.insertTask(task);
		}
		return task;
	};

	/**
	 * @param removedTask
	 * @return true if task is removed correctly
	 */
	public boolean removeTask(Task removedTask) {
		boolean success = false;
		if (removedTask != null) {
			TaskDAO taskDAO = new TaskDAOImp();
			success = taskDAO.removeTask(removedTask.getTaskNumber());
		}
		return success;
	}

	/**
	 * @param updatedTask
	 * @param newTask
	 * @return true if task is updated correctly
	 */
	public boolean updateTask(Task updatedTask, Task newTask) {
		boolean success = false;
		if (newTask != null) {
			updatedTask.updateTask(newTask);
			TaskDAO taskDAO = new TaskDAOImp();
			success = taskDAO.updateTask(updatedTask);
		}
		return success;
	}

	/**
	 * @param act
	 * @return Vector<Task> all task informations of chosen activity
	 */
	public Vector<Task> getTaskOfActivity(Activity act) {
		TaskDAO taskDAO = new TaskDAOImp();
		return taskDAO.getTasksOfActivity(act.getActNumber());
	}

	/**
	 * @param resourceID
	 * @return Vector<Task> all task informations of chosen resource
	 */
	public Vector<Task> getTaskOfResource(int resourceID) {
		TaskDAO taskDAO = new TaskDAOImp();
		return taskDAO.getTasksOfResource(resourceID);
	}

	/**
	 * @param task
	 * @param rs
	 *            method to call unassigns resource methods of model layer
	 * @return true if chosen resource is unassigned from chosen task
	 *         successfully
	 */
	public boolean unassignResourceFromTask(Task task, Resource rs) {
		boolean success = false;
		if (task != null && rs != null) {
			TaskDAO taskDAO = new TaskDAOImp();
			success = taskDAO.unassignResourceFromTask(task.getTaskNumber(),
					rs.getResourceID());
		}
		return success;
	}

	/**
	 * @param task
	 * @param rs
	 *            method to call assigns resource methods of model layer
	 * @return true if chosen resource is assigned to chosen task successfully
	 */
	public boolean assingResourceToTask(Task task, Resource rs) {
		boolean success = false;
		if (task != null && rs != null) {
			TaskDAO taskDAO = new TaskDAOImp();
			success = taskDAO.assignResourceToTask(task.getTaskNumber(),
					rs.getResourceID());
		}
		return success;
	}
     /**
      * This method is required for controlling that  the resource was assigned before
      * this task
     * @param task
     * @param rs
     * @return true if the resource is not assigned to this task before , otherwise return true;
     */
    public boolean assignResourceToTaskControl(Task task , Resource rs){
    	 boolean success = false;
 		if (task != null && rs != null) {
 			TaskDAO taskDAO = new TaskDAOImp();
 			success = taskDAO.assignResourceControl(task.getTaskNumber(), rs.getResourceID());
 		}
 		return success;
     }
	/**
	 * method to get all system resources
	 * 
	 * @return Vector<Person>
	 */
	public Vector<Person> getAllResource() {
		PersonDAO personDAO = new PersonDAOImp();
		Vector<Person> resources = personDAO.getAllResource();
		return resources;
	}

	/**
	 * method to add new person to resource
	 * 
	 * @param person
	 * @return true if new person is added to resources successfully
	 */
	public boolean addPersonResource(Person person) {
		boolean success = false;
		if (person != null) {
			PersonDAO personDAO = new PersonDAOImp();
			success = personDAO.insertResource(person);
		}
		return success;
	}

	/**
	 * method to get resources of task
	 * 
	 * @param task
	 * @return Vector<Person> all resource informations of chosen task
	 */
	public Vector<Person> getResourceOfTask(Task task) {
		if (task != null) {
			PersonDAO personDAO = new PersonDAOImp();
			return personDAO.getResourceOfTask(task.getTaskNumber());
		}
		return null;
	}

	/**
	 * This method provide reporting all projects details in excel or csv format
	 * @param fileType
	 */
	public void reportProjects(String fileType) {
		ProjectDAO projectDAO = new ProjectDAOImp();
		Vector<Project> projects = projectDAO.getProjects();
		ActivityDAO activityDAO = new ActivityDAOImp();
		Vector<Activity> activities = new Vector<Activity>();

		TaskDAO taskDAO = new TaskDAOImp();
		Vector<Task> tasks = new Vector<Task>();
		PersonDAO personDAO = new PersonDAOImp();
		Vector<Person> resources = new Vector<Person>();
		for (int i = 0; i < projects.size(); i++) {
			Project newProject = projects.get(i);
			Vector<Activity> newProjectActivities = activityDAO
					.getActivitiesOfProject(newProject.getProjectID());
			if(newProjectActivities != null && newProjectActivities.size() > 0){
			activities.addAll(newProjectActivities);
			newProject.setProjectActs(newProjectActivities);
			for (Activity newActivity : newProjectActivities) {
				Vector<Task> newActivityTasks = taskDAO
						.getTasksOfActivity(newActivity.getActNumber());
				if (newActivityTasks != null && newActivityTasks.size() > 0) {
					newActivity.setActivityTasks(newActivityTasks);
					tasks.addAll(newActivityTasks);
				}
				for (Task task : newActivityTasks) {
					Vector<Person> newResources = personDAO
							.getResourceOfTask(task.getTaskNumber());
					if (newResources != null && newResources.size() > 0) {
						resources.addAll(newResources);
						task.setTaskResource(newResources);
					}
				}
			}
		  }
		}
		if (fileType.equals("csv")) {
			Report report = new Report("project.csv");
			report.reportProjectToCSV(projects, activities, tasks);
		} else if (fileType.equals("xls")) {
			Report report = new Report("project.xls");
			report.reportProjectToExcel(projects);
		} else if(fileType.equals("console")){
			ReportMenu report = new ReportMenu();
			report.reportProjectToConsole(projects, activities, tasks,resources);
		}
	}

}
